#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/workqueue.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/slab.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/input.h>
#include <linux/of.h>
#include "axp-core.h"
#include "axp-powerkey.h"

static irqreturn_t axp_key_up_handler(int irq, void *data)
{
	struct axp_powerkey_info *info = (struct axp_powerkey_info *)data;
	AXP_DEBUG(AXP_INT, info->chip->pmu_num, "power key up\n");
	input_report_key(info->idev, KEY_POWER, 0);
	input_sync(info->idev);
	return IRQ_HANDLED;
}

static irqreturn_t axp_key_down_handler(int irq, void *data)
{
	struct axp_powerkey_info *info = (struct axp_powerkey_info *)data;
	AXP_DEBUG(AXP_INT, info->chip->pmu_num, "power key down\n");
	input_report_key(info->idev, KEY_POWER, 1);
	input_sync(info->idev);
	return IRQ_HANDLED;
}

static irqreturn_t axp_presslong_handler(int irq, void *data)
{
	struct axp_powerkey_info *info = (struct axp_powerkey_info *)data;
	AXP_DEBUG(AXP_INT, info->chip->pmu_num, "power key long\n");
	input_report_key(info->idev, KEY_POWER, 1);
	input_sync(info->idev);
	ssleep(2);
	input_report_key(info->idev, KEY_POWER, 0);
	input_sync(info->idev);
	return IRQ_HANDLED;
}

static irqreturn_t axp_pressshort_handler(int irq, void *data)
{
	struct axp_powerkey_info *info = (struct axp_powerkey_info *)data;
	AXP_DEBUG(AXP_INT, info->chip->pmu_num, "power key short\n");
	input_report_key(info->idev, KEY_POWER, 1);
	input_sync(info->idev);
	msleep(100);
	input_report_key(info->idev, KEY_POWER, 0);
	input_sync(info->idev);
	return IRQ_HANDLED;
}

struct axp_interrupts axp_powerkey_irq[4] = {
	{"PEK_DBR", axp_key_up_handler},
	{"PEK_DBF", axp_key_down_handler},
	{"PEK_LONG", axp_presslong_handler},
	{"PEK_SHORT", axp_pressshort_handler},
};

int axp_powerkey_dt_parse(struct device_node *node,
			struct axp_config_info *axp_config)
{
	if (!of_device_is_available(node)) {
		pr_err("%s: failed\n", __func__);
		return -1;
	}

	AXP_OF_PROP_READ(pmu_powkey_off_time,            6000);
	AXP_OF_PROP_READ(pmu_powkey_off_func,               0);
	AXP_OF_PROP_READ(pmu_powkey_off_en,                 1);
	AXP_OF_PROP_READ(pmu_powkey_off_delay_time,         0);
	AXP_OF_PROP_READ(pmu_powkey_long_time,           1500);
	AXP_OF_PROP_READ(pmu_powkey_on_time,             1000);
	AXP_OF_PROP_READ(pmu_pwrok_time,                   64);
	AXP_OF_PROP_READ(pmu_pwrnoe_time,                2000);

	return 0;
}
EXPORT_SYMBOL_GPL(axp_powerkey_dt_parse);
